import { Component, OnInit, ChangeDetectorRef, ViewChild, forwardRef, Inject, Output, EventEmitter } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { path } from '../../../../../../path.config';
import { masterService } from '../../../../../../../lib/master.service';
import { HomeComponent } from '../../../../../home.component';
import { AppComponent } from '../../../../../../app.component';

@Component({
    selector: 'app-select-image-add-grid2',
    templateUrl: './select-image-add-grid.component.html',
    styleUrls: ['./select-image-add-grid.component.css']
})
export class SelectImageAddGridComponent implements OnInit {
    // api
    // 获取图片列表
    private getImgList = path.materialManagement.imageManagement.getImgList;

    // 列表项数组
    public rows: Array<any> = [];
    // 表格数据头部标题
    public columns: Array<any> = [
        { title: 'ID', name: 'id' },
        { title: '图片', name: 'imageUrl' },
        { title: '名称', name: 'imageName' },
    ];
    // 当前页码
    public page: any = 1;
    // 一共多少页
    public itemsPerPage: number = 10;
    // 分页最多显示的页数
    public maxSize: number = 5;
    // 总页数计数变化时，事件数等于总页数计数
    public numPages: number = 1;
    // 总共的列表项数
    public totalItems: number = 0;
    // 分页器的文本配置
    public localText = {
        firstBtn: '首页',
        lastBtn: '末页',
        previousBtn: '上一页',
        nextBtn: '下一页'
    }

    @Output() closeModal = new EventEmitter<any>();

    constructor(public _masterService: masterService,
        public cdRef: ChangeDetectorRef,
        @Inject(forwardRef(() => AppComponent)) public app: AppComponent) { }

    ngOnInit() {
        this.onChangeTable();
    }

    // 渲染表格
    public onChangeTable(page: any = {
        itemsPerPage: this.itemsPerPage,
        page: this.page
    }): any {
        // get传入的参数
        let urlObject = {
            pageSize: page.itemsPerPage,
            pageIndex: page.page
        }
        this._masterService
            .getData(this.getImgList, urlObject)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.rows = res.data.items;
                    this.totalItems = res.data.pagination.totalCount;
                    this.itemsPerPage = res.data.pagination.pageSize;
                    this.page = parseInt(res.data.pagination.pageIndex);
                    this.cdRef.detectChanges();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    selectImage(Id, Url) {
        let obj = {
            id: Id,
            url: Url
        }
        this.closeModal.emit(obj);
    }
}
